﻿Imports System.Data
Imports System.Data.SqlClient
Public Class ProvSistemaDAO
    Implements ISistema

    Private connection As SqlConnection

    Public Sub New(ByVal connection As SqlConnection)
        Me.connection = connection
    End Sub

    Public Function AtualizarSistema(ByVal parIdSistema As Integer, ByVal parNovoSistema As SuporteModel.Sistema) As Object Implements ISistema.AtualizarSistema

    End Function

    Public Function CadastrarSistema(ByVal parSistema As SuporteModel.Sistema) As Boolean Implements ISistema.CadastrarSistema

    End Function

    Public Function ExcluirSistema(ByVal parIdSistema As Integer) As Boolean Implements ISistema.ExcluirSistema

    End Function

    Public Function ListarSistema() As System.Collections.Generic.List(Of SuporteModel.Sistema) Implements ISistema.ListarSistema
        Dim listaSistemas As New List(Of SuporteModel.Sistema)
        Try
            Me.connection.Open()
            Dim comm As New SqlCommand("select SIS_CODIGO,SIS_NOME,SIS_INATIVO from SISTEMAS", Me.connection)
            Dim dataReader As SqlDataReader = comm.ExecuteReader()

            While dataReader.Read()
                Dim sistema As New SuporteModel.Sistema
                sistema.IdSistema = Convert.ToUInt64(dataReader("SIS_CODIGO"))
                sistema.Inativo = Convert.ToBoolean(dataReader("SIS_INATIVO"))
                sistema.Nome = dataReader("SIS_NOME").ToString()
                listaSistemas.Add(sistema)
            End While
        Catch ex As Exception

        Finally
            Me.connection.Close()
        End Try
        Return listaSistemas
    End Function

    Public Function CadastrarTela(ByVal parTela As SuporteModel.Tela) As Boolean Implements ISistema.CadastrarTela
        Try
            Me.connection.Open()

        Catch ex As Exception
        Finally
            Me.connection.Close()
        End Try
    End Function

    Public Function ListarTela() As System.Collections.Generic.List(Of SuporteModel.Tela) Implements ISistema.ListarTela
        Dim listaTelas As New List(Of SuporteModel.Tela)
        Try
            Me.connection.Open()
            Dim comm As New SqlCommand("select TEL_CODIGO,TEL_NOME,TEL_INATIVA,TEL_DOCUMENTACAO from TELAS", Me.connection)
            Dim dataReader As SqlDataReader = comm.ExecuteReader()
            While dataReader.Read()
                Dim tela As New SuporteModel.Tela
                tela.Documentacao = dataReader("TEL_DOCUMENTACAO").ToString()
                tela.Inativa = Convert.ToBoolean(dataReader("TEL_INATIVA"))
                tela.NomeTela = dataReader("TEL_NOME").ToString()
                tela.IdTela = Convert.ToInt64(dataReader("TEL_CODIGO"))
                listaTelas.Add(tela)
            End While
        Catch ex As Exception
        Finally
            Me.connection.Close()
        End Try
        Return listaTelas
    End Function

    Public Function BuscarSistema(ByVal parIdSistema As Integer) As SuporteModel.Sistema Implements ISistema.BuscarSistema
        Dim sistema As New SuporteModel.Sistema
        Try
            Me.connection.Open()
            Dim comm As New SqlCommand("SELECT  CASE SIS_INATIVO WHEN 0 THEN 0 WHEN 1 THEN 1 WHEN NULL THEN 1 END AS INATIVO, SIS_NOME,SIS_CODIGO FROM SISTEMAS WHERE SIS_CODIGO = @codigo", Me.connection)
            comm.Parameters.AddWithValue("@codigo", parIdSistema)
            Dim dataReader As SqlDataReader = comm.ExecuteReader()
            If dataReader.Read() Then
                sistema.IdSistema = Convert.ToInt64(dataReader("SIS_CODIGO"))
                Try
                    sistema.Inativo = Convert.ToBoolean(dataReader("SIS_INATIVO"))
                Catch ex As Exception
                End Try

                sistema.Nome = Convert.ToString(dataReader("SIS_NOME"))
            End If
        Catch ex As Exception
        Finally
            Me.connection.Close()
        End Try
        Return sistema
    End Function
End Class
